package com.czy.utilslib.view.viewPager;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 单个显示的Fragment
 * @param <VB>      单个显示Fragment的ViewBinding
 */
public abstract class SinglePagerFragment<VB extends ViewBinding> extends Fragment {

    protected static final String ARG_POSITION = "position";
    protected int currentPosition;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            currentPosition = getArguments().getInt(ARG_POSITION);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initViewBinding(inflater, container);

        init();

        setListener();

        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 在二级界面返回之后，position可能变化，所以需要再次调用变化情况
        initPositionData();
    }

    //-------------------------------init-------------------------------

    protected void init(){
        initPositionData();
    }

    //-------------------------------Listener-------------------------------

    // 设置监听逻辑
    protected void setListener() {

    }

    //-------------------------------viewBinding-------------------------------

    protected VB binding;
    // viewBinding反射
    private void initViewBinding(LayoutInflater inflater, ViewGroup container) {
        // 获取泛型的父类型
        Type superclass = getClass().getGenericSuperclass();
        // 获取参数类型
        Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];

        try {
            // 反射获取 inflate 方法
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            // 填充Binding
            binding = (VB) method.invoke(null, inflater, container, false);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    //-------------------------------Instance-------------------------------
    ;
    // 获取单例，以及传递 position 数据
    // 静态方法用于创建实例
    public static <F extends Fragment> F newInstance(Class<F> fragmentClass, int position) {
        F fragment;
        try {
            fragment = fragmentClass.getDeclaredConstructor().newInstance(); // 使用反射创建实例
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
            throw new RuntimeException("Failed to create fragment: " + fragmentClass.getSimpleName(), e);
        } catch (Exception e) {
            throw new RuntimeException("Unexpected exception while creating fragment", e);
        }

        Bundle args = new Bundle();
        args.putInt(ARG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }

    // 切换成当前Fragment之后需要执行的任务
    public abstract void changeToDo(int position);

    //-------------------------------viewPager-------------------------------
    ;
    // position变化之后，要设置变化状况
    public abstract void initPositionData();

}
